//
//  ViewController.m
//  TestCoreData
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "ViewController.h"
#import "CoreDataManager.h"
#import "Student.h"

@interface ViewController ()
- (IBAction)addButton:(id)sender;
- (IBAction)selectAction:(id)sender;
- (IBAction)updateAction:(id)sender;
- (IBAction)deleteAction:(id)sender;


@property(nonatomic, strong)CoreDataManager *manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 考虑到manager可能在很多方法中用, 所有写成属性
    self.manager = [CoreDataManager defaults];
    NSLog(@"url ======= %@", [self.manager applicationDocumentsDirectory]);
    [self.manager applicationDocumentsDirectory];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// coreData添加方法
- (IBAction)addButton:(id)sender {
    // 创建一个实体描述类(NSEntityDescription)
    // 参数1: 描述的实体
    // 参数2: 数据管理器
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    // 创建实体
    // 参数1: 实体描述
    // 参数2: 数据管理器
    Student *stu = [[Student alloc] initWithEntity:entityDescription insertIntoManagedObjectContext:self.manager.managedObjectContext];
    stu.name = @"根";
    stu.age = @12345;
    stu.sex = @"?";
    stu.number = @100;
    stu.hobby = @"吹";
    
    [self.manager saveContext];
}

// coreData 查找
- (IBAction)selectAction:(id)sender {
    // 获取查询数据的请求, 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    // 添加约束  谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"根"];
    // 给请求添加约束(理解为SQL中的查找添加)
    request.predicate = predicate;
    
    // 通过某个key给数组排序(因为之前是乱序的)
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    NSLog(@"%ld", array.count);
}

// coreData 修改
- (IBAction)updateAction:(id)sender {
    // 获取查询数据的请求, 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    // 添加约束  谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"根"];
    // 给请求添加约束(理解为SQL中的查找添加)
    request.predicate = predicate;
    
    // 通过某个key给数组排序(因为之前是乱序的)
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    Student *stu = [array lastObject];
    stu.sex = @"好像大概应该差不多可能约么也许是个女的";
    // 不要忘了保存
    [self.manager saveContext];
}

// coreData删除
- (IBAction)deleteAction:(id)sender {
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"sex", @"%@"], @"female"];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sex"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    for (Student *stu in fetchedObjects) {
        [self.manager.managedObjectContext deleteObject:stu];
        [self.manager saveContext];
    }
}




@end
